package logic.enemies;

import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import logic.blocks.Block;
import logic.characters.Player;
import logic.entities.GameObject;
import logic.pruebas.GamePanel;

public abstract class Enemy extends GameObject{
	private static final long serialVersionUID = 7388621616208602380L;
	public int life;
	@SuppressWarnings("unused")
	private int difficulty;
	public int attack;
	public int defense;
	public int magic;
	public int resistance;
	public int speed;
	public int originalSpeed;
	public int jump;
	public int originalJump;
	public int stamina;
	private final float GRAVITY = 1000f;
	public boolean relevant = false;
	public boolean onPlatform = false;
	public boolean falling = true;
	private boolean vulnerable = true;
	public float velX;
	public float velY;
	private long lastDamage;
	public int experience;
	public ArrayList<Enemy> enemies;
	public ArrayList<Block> blocks;
	public Player player;
	public boolean lookingRight = false;
	public boolean sleeping = false;
	public long sleepTime;

	public Enemy(float x, float y, GamePanel panel, String nomImg, int difficulty, ArrayList<Enemy> enemies, ArrayList<Block> blocks, Player player) {
		super(x, y, panel, nomImg);
		dead = false;
		this.enemies = enemies;
		this.blocks = blocks;
		this.player = player;
	}

	public void update(Player player) {
		if (!relevant) {
			checkRelevant();
		} else if (relevant) {
		
		if (sleeping) {
			wakeUp();
			dibujar();
		}
		
		if (!dead && !sleeping) {
			collisionX = coorX;
			collisionY = coorY;
			collisionWidth = anchura;
			collisionHeight = altura;
			
			if (velX < 0) {
				lookingRight = false;
			} else if (velX > 0) {
				lookingRight = true;
			}
			
			
			
			if (!vulnerable) {
				if (System.currentTimeMillis() - lastDamage >= 500) {
					vulnerable = true;
				}
			}
			if (dibujado) {
				if (!onPlatform) {
					falling = true;
				}
				float tiempoTranscurridoEnSegundos = 
						(System.currentTimeMillis() - ultimoDibujo) / 1000.0F;
				if (tiempoTranscurridoEnSegundos > 0.01F) {
					if(falling) {
						velY = velY + GRAVITY * tiempoTranscurridoEnSegundos;
					}
						coorX = coorX + velX * tiempoTranscurridoEnSegundos;
						coorY = coorY + velY * tiempoTranscurridoEnSegundos;
				}
			}
			dibujar();
		}
		}
	}

	public void impactoJugador(Player jugador) {
		if (dibujado && !dead && !sleeping) {
			if (jugador.dibujado && !jugador.dead) {
				Line2D pUp = new Line2D.Float(jugador.collisionX + 5, jugador.collisionY, jugador.collisionX + jugador.collisionWidth - 7, jugador.collisionY);
				Line2D pDown = new Line2D.Float(jugador.collisionX + 5, jugador.collisionY + jugador.collisionHeight, jugador.collisionX + jugador.collisionWidth - 7, jugador.collisionY + jugador.collisionHeight);
				Line2D pLeft = new Line2D.Float(jugador.collisionX, jugador.collisionY + 5, jugador.collisionX, jugador.collisionY + jugador.collisionHeight - 7);
				Line2D pRight = new Line2D.Float(jugador.collisionX + jugador.collisionWidth, jugador.collisionY + 5, jugador.collisionX + jugador.collisionWidth, jugador.collisionY + jugador.collisionHeight - 7);
				Rectangle object = new Rectangle((int)collisionX, (int)collisionY, collisionWidth, collisionHeight);

				if (pDown.intersects(object)) {
					downImpact(jugador);
				} else if (pUp.intersects(object)) {
					upImpact(jugador);
				} else if (pLeft.intersects(object)) {
					leftImpact(jugador);
				} else if (pRight.intersects(object)) {
					rightImpact(jugador);
				}
			}
		}
	}

	public void updateIcon (String nomImg) {
		URL imgURL = getClass().getClassLoader().getResource(nomImg);
		icono = new ImageIcon(imgURL);
		setIcon( icono );
		setSize(icono.getIconWidth(), icono.getIconHeight());
	}

	public void damage (int amount) {
		if (vulnerable) {
			int damage = amount - defense;
			if (damage < 0) {
				damage = 0;
			}
			life = life - damage;
			if (life <= 0) {
				die();
			}
			vulnerable = false;
			lastDamage = System.currentTimeMillis();
		}
	}

	public void die() {
		velX = 0;
		velY = 0;
		panelJuego.remove(this);
		panelJuego.updateUI();
		dibujado = false;
		dead = true;
	}

	public void checkRelevant() {
		if (!seSaleX() && !seSaleY()) {
			relevant = true;
		}
	}
	
	public void magicDamage(int amount) {
		if (vulnerable) {
		int damage = amount - resistance;
		if (damage < 0) {
			damage = 0;
		}
		this.life -= damage;
		if (life <= 0) {
			die();
		}
	}
		vulnerable = false;
		lastDamage = System.currentTimeMillis();
	}
	
	public void jump() {
		velY = -250 * jump;
		falling = true;
		jump = originalJump;
		speed = originalSpeed;
	}
	
	public void wakeUp() {
		long now = System.currentTimeMillis();
		if (now - sleepTime >= 2500) {
			sleeping = false;
		}
	}
	
	public abstract void upImpact(Player player);
	public abstract void downImpact(Player player);
	public abstract void rightImpact(Player player);
	public abstract void leftImpact(Player player);
	
}

